/**
 * 
 * @Project le cOeur - Data Virtualization
 * @Date 08.12.2012
 * @Author Aebi Pascal, Kocher Marc
 * @School BFH-TI Biel, Bachelor Thesis
 *
 */

package main.implementation.representation;

import java.util.List;

import main.framework.http.ContentType;
import main.framework.http.MessageBody;
import main.framework.representation.RepresentationCreator;
import main.framework.resource.Resource;
import main.framework.resource.ResourceData;

/**
 * This class represents a representation creator for the HTML content type.
 */
public class HTMLRepresentationCreator extends RepresentationCreator {

	/**
	 * Creates a representation creator for the HTML content type.
	 * 
	 * @param contentType The desired content type for the representations to be created by the representation creator.
	 */
	public HTMLRepresentationCreator(ContentType contentType) {
		super(contentType);
	}

	@Override
	public MessageBody createRepresentation(String data) {
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
		stringBuilder.append("<html xmlns='http://www.w3.org/1999/xhtml'>\n");
		stringBuilder.append("<head><title>le cOeur</title></head>\n");
		stringBuilder.append("<body>\n");
		stringBuilder.append("<p>");
		stringBuilder.append(data);
		stringBuilder.append("</p>\n");
		stringBuilder.append("</body>\n");
		stringBuilder.append("</html>");
		return new MessageBody(stringBuilder.toString(), super.contentType);
	}

	@Override
	public MessageBody createRepresentation(ResourceData resourceData) {
		String uriNode = resourceData.getResource().getURINode().getUriNode();
        List<Resource> childrenResources = resourceData.getResource().getChildren();
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
		stringBuilder.append("<html xmlns='http://www.w3.org/1999/xhtml'>\n");
		stringBuilder.append("<head><title>le cOeur</title></head>\n");
		stringBuilder.append("<body>\n");
		stringBuilder.append("<h1>");
		stringBuilder.append(uriNode);
		stringBuilder.append("</h1>\n");
		for (int i = 0; i < resourceData.getData().size(); i++) {
			for (int j = 0; j < resourceData.getData().get(i).length; j++){
				if(childrenResources.size() == 0){
					// last node --> data comes from SQL query, first row are the column names
					if (i > 0){
						stringBuilder.append("<p>");
						stringBuilder.append(resourceData.getData().get(0)[j]);
						stringBuilder.append(" = ");
						stringBuilder.append(resourceData.getData().get(i)[j]);
						stringBuilder.append("</p>\n");
					}
				} else {
					stringBuilder.append("<p><a href=\"");
					stringBuilder.append(childrenResources.get(i).getResourceURI());
					stringBuilder.append("\">");
					stringBuilder.append(childrenResources.get(i).getURINode().getUriNode());
					stringBuilder.append("</a></p>\n");
				}
			}
		}
		stringBuilder.append("</body>\n");
		stringBuilder.append("</html>");
		return new MessageBody(stringBuilder.toString(), super.contentType);
	}

}
